#课堂练习

#定义一个打印方法
def printMethod(obj):
    print("\n我创建了一个球")
    print("这个球大小是：",obj.size," 颜色是：",obj.color," 球的方向是：",obj.direction)
    print("现在我向下拍这个球")
    obj.bounce()
    print("拍完球后，现在球的方向是：",obj.direction)

#定义一个球类
class Ball:
    #定义弹跳方法
    def bounce(self):
        if self.direction=='down':
            self.direction='up'

#创建球类的实例且设置属性
myball=Ball()
myball.direction='down'
myball.color='green'
myball.size='small'

# print("\n我创建了一个球")
# print("这个球大小是：",myball.size," 颜色是：",myball.color," 球的方向是：",myball.direction)
# print("现在我向下拍这个球")
# myball.bounce()
# print("拍完球后，现在球的方向是：",myball.direction)
printMethod(myball)



#给类增加初始化（__init__()）方法
class Ball2:
    #初始化
    def __init__(self,color,size,direction):
        self.color=color
        self.size=size
        self.direction=direction

    #定义拍球方法
    def bounce(self):
        if self.direction=='down':
            self.direction='up'

    #覆写打印方法
    def __str__(self) -> str:
        msg="\n你好,我是一个球 尺寸是："+self.size+" 颜色是："+self.color+" 方向是："+self.direction
        return msg

myball2=Ball2('red','samll','down')

# print("\n我创建了一个球2")
# print("这个球2大小是：",myball2.size," 颜色是：",myball2.color," 球的方向是：",myball2.direction)
# print("现在我向下拍这个球2")
# myball2.bounce()
# print("拍完球2后，现在球2的方向是：",myball2.direction)

printMethod(myball2)
print(myball2)
print(myball2.__str__())

#热狗示例
class HotDog:
    def __init__(self):
        self.cooked_level=0
        self.cooked_string="生的"
        self.condiments=[]

    def cook(self,time):
        self.cooked_level=self.cooked_level+time
        if self.cooked_level>8:
            self.cooked_string='烤糊'
        elif self.cooked_level>5:
            self.cooked_string='烤熟'
        elif self.cooked_level>3:
            self.cooked_string='夹生'
        else:
            self.cooked_string='生的'

    def addCondiment(self,condiment):
        self.condiments.append(condiment)

    def __str__(self):
        msg="热狗"
        if len(self.condiments)>0:
            msg=msg+" 添加 "
        for i in self.condiments:
            msg=msg+i+", "
        msg=msg.strip(", ")
        msg=self.cooked_string+" "+msg+"."

        return msg

myDog=HotDog()
print(myDog.cooked_level)
print(myDog.cooked_string)
print(myDog.condiments)

# print("\n现在我们来烤热狗")
# print("烤热狗 4 分钟")
# myDog.cook(4)
# print(myDog.cooked_level)
# print(myDog.cooked_string)
# print(myDog.condiments)

print("\n烤热狗测试")
print("\n还没烤热狗时")
print(myDog)
print("\n先烤热狗4分钟")
myDog.cook(4)
print(myDog)
print("\n在烤热狗3分钟")
myDog.cook(3)
print(myDog)
print("\n最后在烤热狗烤10分钟将会发生什么？")
myDog.cook(10)
print(myDog)
print("\n现在，我给热狗添加一些调料：")
myDog.addCondiment('番茄酱')
myDog.addCondiment('芥末') 
print(myDog)

#多态（同名方法可以在不同类中有不同表现）

#三角形
class Triangle:
    #初始化（即构造函数）
    def __init__(self,width,height):
        self.width=width
        self.height=height
    
    #获取面积
    def getArea(self):
        area=(self.width*self.height)/2
        return area

#正方形
class Square:
    #初始化（即构造函数）
    def __init__(self,width,height):
        self.width=width
        self.height=height

    #获取面积
    def getArea(self):
        area=self.width*self.height
        return area
        

triangle = Triangle(5,6)
print("三角形的面积：",triangle.getArea())

square=Square(5,6)
print("正方形的面积：",square.getArea())

#继承
class GameObject:
    def __init__(self,name):
        self.name=name

    def pickUp(self):
        #
        print("调用【GameObject】类的捡起方法")

class Coin(GameObject):
    def __init__(self, name):
        super().__init__(name)
    
    def spend(self):
        print("Coin测试 ",self.name)
   
        

newCoin=Coin("我是一个金币")
print("\n开始调用金币的Spend方法")
newCoin.spend()
print("\n开始调用父类的pickUp方法")
newCoin.pickUp()


        




